package com.yuejie.musicai.db;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.yuejie.musicai.log.LogLevelEnum;
import com.yuejie.musicai.log.LogServiceImpl;

public class DBConnection {
	private Connection conn;

	private static Properties props;

	/**
	 * 获取连接
	 * 
	 * @throws Exception
	 */
	public DBConnection()  
	{
		try 
		{
			Class.forName(getProperty("driver-class"));
			conn = DriverManager.getConnection(getProperty("url"),
					getProperty("user-name"), getProperty("password"));
			
			conn.setAutoCommit(false);
		} 
		catch (Exception e) 
		{
			LogServiceImpl.log(LogLevelEnum.ERROR, e.toString());
			e.printStackTrace();
		}
	}

	/**
	 * 插入操作
	 * 
	 * @param objmaps
	 * @return
	 * @throws Exception
	 */
	public boolean insertSMSInfo(String strSQL,List<Object[]> objList) throws Exception 
	{
		boolean blnresult = false;
		try 
		{
			PreparedStatement pstmtInsert = conn.prepareStatement(strSQL);
			for (int i = 0; i < objList.size(); i++) 
			{
				int objnum = ((Object[])objList.get(i)).length;
				for(int j = 0; j < objnum; j++)
				{
					pstmtInsert.setObject(j+1, ((Object[])objList.get(i))[j]);
				}
				pstmtInsert.addBatch();
			}
			pstmtInsert.executeBatch();
			conn.commit();
			pstmtInsert.close();
			blnresult = true;
		} 
		catch (Exception ex) 
		{
			conn.rollback();
			throw ex;
		} 
		return blnresult;
	}
	
	/**
	 * 插入操作
	 * 
	 * @param objmaps
	 * @return
	 * @throws Exception
	 */
	public boolean executeBatchInfo(String[] strSQL,List<Object[]> objList) throws Exception 
	{
		boolean blnresult = false;
		try 
		{
			PreparedStatement pstmtInsert = null;
			for(int n = 0; n < strSQL.length; n++)
			{
			    pstmtInsert = conn.prepareStatement(strSQL[n]);

				int objnum = ((Object[])objList.get(n)).length;
				for(int j = 0; j < objnum; j++)
				{
					pstmtInsert.setObject(j+1, ((Object[])objList.get(n))[j]);
				}
				pstmtInsert.addBatch();
				
				pstmtInsert.executeBatch();
			}
			conn.commit();
			pstmtInsert.close();
			blnresult = true;
		} 
		catch (Exception ex) 
		{
			conn.rollback();
			throw ex;
		} 
		return blnresult;
	}
	
	/**
	 * 查询
	 * 
	 * @param objmaps
	 * @return
	 * @throws Exception
	 */
	public List<Map<String, Object>> querySMSInfo(String strSQL,Object[] params) throws Exception 
	{
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		try 
		{
			PreparedStatement pstmtQuery = conn.prepareStatement(strSQL);
			for (int i = 0; i < params.length; i++) 
			{
				pstmtQuery.setObject(i+1, params[i]);
			}
			ResultSet rs = pstmtQuery.executeQuery();
			ResultSetMetaData rsmd = rs.getMetaData();
			int col = rsmd.getColumnCount();
			if( rs != null )
			{
				while(rs.next())
				{
					String attName = "";
					Map<String, Object> map = new HashMap<String, Object>();
					for(int j = 1; j <= col; j++)
					{
						attName = rsmd.getColumnName(j);
						attName = attName.toLowerCase();
						if(rs.getObject(j) != null)
						{
							map.put(attName, rs.getObject(j));
						}
						else
						{
							map.put(attName, "");
						}		    
					}
					resultList.add(map);
				}
			}
			pstmtQuery.close();
		} 
		catch (Exception ex) 
		{
			throw ex;
		} 
		return resultList;
	}
	
	/**
	 * 
	* 关闭连接
	* @param conn    设定文件
	* @return void    返回类型
	* @throws
	 */
	public void close()
	{
		try 
		{
			if(conn != null)
			{
			    conn.close();
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}

	private String getProperty(String key) throws Exception 
	{
		if (props == null) 
		{
			java.io.InputStream ins = com.yuejie.musicai.db.DBConnection.class
					.getResourceAsStream("config.properties");
			props = new Properties();
			try 
			{
				props.load(ins);
			} 
			catch (IOException e) 
			{
				throw new Exception("读取配置文件失败！");
			}
		}
		return props.getProperty(key);
	}
}
